package com.fang.unittest;

import java.io.*;

public class TestDriver {
    //分别使用输入流、输出流、字节流、字符流以及缓冲流实现文件复制。
    public static void main(String[] args) {
        test03();
    }

    /**
     * 使用字节流进行文件传输
     */
    private static void test01(){
        try {
            InputStream is = new FileInputStream("D:\\java\\lib\\yyy.txt");
            byte[] b = new byte[1024];
            int len = is.read(b);
            OutputStream os = new FileOutputStream("D:\\java\\lib\\xxx.txt");
            while (len != -1){
                os.write(b,0,len);
                len = is.read(b);
            }
            is.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 使用字符流进行文件的复制
     *
     */
    private static void test02(){
        try {
            Reader re = new FileReader("D:\\java\\lib\\yyy.txt");
            Writer wr = new FileWriter("D:\\java\\lib\\zzz.txt");
            char[] c = new char[1024];
            int len = re.read(c);
            while(len != -1){
                wr.write(c,0,len);
                len = re.read(c);
            }
            re.close();
            wr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void test03(){
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\java\\lib\\yyy.txt")));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\java\\lib\\aaa.txt")));

            String str = br.readLine();
            while(str != null){
                bw.write(str);
                bw.newLine();
                str = br.readLine();
            }
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
